package MVC.controller;

import java.io.IOException;
import java.util.ArrayList;

import myAsm.MyClassNode;
import myAsm.MyJarNode;
import myAsm.MyMethodNode;
import myAsm.MyPackageNode;
import MVC.Exceptions.CPException;
import MVC.model.ModelePatternFinder;
import MVC.view.JTreeWindow;


public class ControllerPatternFinder implements IController{

	private JTreeWindow view;
	private ModelePatternFinder mpf;

	public ControllerPatternFinder(){
		this.mpf = new ModelePatternFinder();
		this.view = new JTreeWindow(this);
	}
	
	
	public void setMpf(ModelePatternFinder mpf) {
		this.mpf = mpf;
	}

	public ModelePatternFinder getMpf() {
		return mpf;
	}
	
	//Sauver le Jar
	public void notifySaveJar(final String nameJar){
	        try {
				mpf.fillDataBase(nameJar,this.view);
			} catch (IOException e) {
				view.buildErrorBox(e.getMessage());
			}   
	}
	
	//Get des listes de packages/classes/methodes
	public ArrayList<MyJarNode> getJarList(){ 
		return mpf.createJarList();	
	}
	
	public ArrayList<MyPackageNode> getPackageList(String jarID){ 
		return mpf.createPackageList(jarID);	
	}
	
	public ArrayList<MyClassNode> getClassList(String packageID){ 
		return mpf.createClassList(packageID);	
	}
	
	public ArrayList<MyMethodNode> getMethodList(String classID){ 
		return mpf.createMethodList(classID);
	}
	
	

	public void notifyRunCheck(ArrayList<String> testsToRun,String ToTest ){
		String[] tt = ToTest.split("__");
		int level = Integer.parseInt(tt[0]);
		ToTest = tt[1];
		switch(level){ 
			case 1 :
				try {
					mpf.RunCheck(testsToRun, ToTest,"jar",this.view,this.view);
				} catch (CPException e) {
					view.buildErrorBox(e.getMessage());
					System.out.println("Notifiy error");
				}
				break;
			case 2 :
				try {
					mpf.RunCheck(testsToRun, ToTest,"package",this.view,this.view);
				} catch (CPException e) {
					view.buildErrorBox(e.getMessage());
				}
				break;
			case 3 :
				try {
					mpf.RunCheck(testsToRun, ToTest,"class",this.view,this.view);
				} catch (CPException e) {
					view.buildErrorBox(e.getMessage());
				}
				break;
			case 4 :
				try {
					mpf.RunCheck(testsToRun, ToTest,"method",this.view,this.view);
				} catch (CPException e) {
					view.buildErrorBox(e.getMessage());
				}
				break;
		}
	}
}